﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

using NullStudio.Fetion_SDK;
using Fetion_SDK.FetionObj;

namespace Fetion_SDK
{
    /// <summary>
    /// 使用飞信的方式
    /// </summary>
    public enum FetionMode
    {
        FetionSDK,
        WebService
    }

    public class FetionHelper
    {
        #region 变量
        private FetionMode fetionMode;
        private FetionSDK sdk;
        private fWebSer fsms;
        #endregion

        #region 属性
        /// <summary>
        /// 飞信账号
        /// </summary>
        private string userId;

        public string UserId
        {
            get { return userId; }
            set { userId = value; }
        }
        private string userPass;

        /// <summary>
        /// 飞信密码
        /// </summary>
        public string UserPass
        {
            get { return userPass; }
            set { userPass = value; }
        }
        #endregion

        #region 构造函数
        public FetionHelper(FetionMode fetionMode)
        {
            this.fetionMode = fetionMode;

            if (this.fetionMode == FetionMode.FetionSDK)
            {
                sdk = new FetionSDK();
            }
            else
            {
                fsms = new fWebSer();
                fsms.CookieContainer = new CookieContainer();
            }
        }
        #endregion

        #region 共用方法
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pass"></param>
        /// <returns></returns>
        public bool Login(string id, string pass)
        {
            bool blnResult = false;

            //保留用户Id和pass
            this.UserId = id;
            this.UserPass = pass;

            try
            {
                if (this.fetionMode == FetionMode.FetionSDK)
                {
                    sdk.AccountManager.FillUserIdAndPassword(id, pass, true);
                    blnResult = sdk.AccountManager.LoginOrLogout();
                }
                else
                {
                    string loginReslut = fsms.Login(id, pass);
                }
            }
            catch (Exception)
            {
                blnResult = false;
            }

            return blnResult;
        }

        /// <summary>
        /// 用户注销
        /// </summary>
        /// <returns></returns>
        public bool Logout()
        {
            bool blnResult = false;

            try
            {
                if (this.fetionMode == FetionMode.FetionSDK)
                {
                    sdk.AccountManager.FillUserIdAndPassword(this.UserId, this.UserPass, false);
                    blnResult = sdk.AccountManager.LoginOrLogout();
                }
                else
                {
                    string loginReslut = fsms.Logout(this.UserId);
                }
            }
            catch (Exception)
            {
                blnResult = false;
            }

            return blnResult;
        }

        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="id"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public int SendSMS(string id, string msg)
        {
            int intResult = 0;

            try
            {
                if (this.fetionMode == FetionMode.FetionSDK)
                {
                    intResult = sdk.ContactControl.SendSMS.SendSMS(id, msg);
                }
                else
                {
                    fsms.Send(this.UserId, id, msg);
                }
            }
            catch (Exception)
            {
                intResult = 0;
            }

            return intResult;
        }

        /// <summary>
        /// 给自己发短信
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public int SendSMSToMySelf(string msg)
        {
            int intResult = 0;

            try
            {
                if (this.fetionMode == FetionMode.FetionSDK)
                {
                    intResult = sdk.ContactControl.SendSMS.SendSMS(sdk.ContactControl.getMyself().Uri.Id, msg);
                }
                else
                {
                    fsms.Send(this.UserId, this.UserId, msg);
                }
            }
            catch (Exception)
            {
                intResult = 0;
            }

            return intResult;
        }

        #endregion
    }
}
